package com.ruoyi.web.service.impl;

import java.util.List;
import java.util.Objects;

import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.service.impl.SysUserServiceImpl;
import com.ruoyi.web.domain.OperatorRecord;
import com.ruoyi.web.domain.Product;
import com.ruoyi.web.mapper.OperatorRecordMapper;
import com.ruoyi.web.service.IOperatorRecordService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


/**
 * 派单Service业务层处理
 *
 * @author song
 * @date 2024-05-17
 */
@Service
public class OperatorRecordServiceImpl implements IOperatorRecordService {
    @Autowired
    private OperatorRecordMapper operatorRecordMapper;

    private static final Logger log = LoggerFactory.getLogger(SysUserServiceImpl.class);

    /**
     * 查询派单
     *
     * @param id 派单主键
     * @return 派单
     */
    @Override
    public OperatorRecord selectOperatorRecordById(Long id) {
        return operatorRecordMapper.selectOperatorRecordById(id);
    }

    /**
     * 查询派单列表
     *
     * @param operatorRecord 派单
     * @return 派单
     */
    @Override
    public List<OperatorRecord> selectOperatorRecordList(OperatorRecord operatorRecord) {
        return operatorRecordMapper.selectOperatorRecordList(operatorRecord);
    }

    /**
     * 新增派单
     *
     * @param operatorRecord 派单
     * @return 结果
     */
    @Override
    public int insertOperatorRecord(OperatorRecord operatorRecord) {
        operatorRecord.setCreateTime(DateUtils.getNowDate());
        return operatorRecordMapper.insertOperatorRecord(operatorRecord);
    }

    /**
     * 修改派单
     *
     * @param operatorRecord 派单
     * @return 结果
     */
    @Override
    public int updateOperatorRecord(OperatorRecord operatorRecord) {
        operatorRecord.setUpdateTime(DateUtils.getNowDate());
        return operatorRecordMapper.updateOperatorRecord(operatorRecord);
    }

    /**
     * 批量删除派单
     *
     * @param ids 需要删除的派单主键
     * @return 结果
     */
    @Override
    public int deleteOperatorRecordByIds(Long[] ids) {
        return operatorRecordMapper.deleteOperatorRecordByIds(ids);
    }

    /**
     * 删除派单信息
     *
     * @param id 派单主键
     * @return 结果
     */
    @Override
    public int deleteOperatorRecordById(Long id) {
        return operatorRecordMapper.deleteOperatorRecordById(id);
    }


    /**
     * 导入派单数据
     *
     * @param recordList      用户数据列表
     * @param isUpdateSupport 是否更新支持，如果已存在，则进行更新数据
     * @param operName        操作用户
     * @return 结果
     */
    @Override
    public String importProduct(List<OperatorRecord> recordList, Boolean isUpdateSupport, String operName) {
        if (StringUtils.isNull(recordList) || recordList.isEmpty()) {
            throw new ServiceException("导入用户数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (OperatorRecord record : recordList) {
            try {
                // 验证是商品号是否存在
                OperatorRecord p = operatorRecordMapper.selectOperatorRecordByProductNo(record.getProductNo());

                if (StringUtils.isNull(p)) {

                    record.setCreateBy(operName);
                    operatorRecordMapper.insertOperatorRecord(record);
                    successNum++;
                    successMsg.append("<br/>").append(successNum).append("、派单 ").append(record.getProductNo()).append(" 导入成功");
                } else if (!Objects.equals(record.getStatus(), p.getStatus()) && !Objects.equals(p.getStatus(), "已完成")) {
                    record.setCreateBy(operName);
                    record.setUpdateBy(operName);
                    operatorRecordMapper.deleteOperatorRecordById(p.getId());
                    operatorRecordMapper.insertOperatorRecord(record);
                    successNum++;
                    successMsg.append("<br/>").append(successNum).append("、派单 ").append(record.getProductNo()).append(" 更新成功");
                } else {


                    failureNum++;
                    failureMsg.append("<br/>").append(failureNum).append("、派单 ").append(record.getProductNo()).append(" 已存在");
                }
            } catch (Exception e) {
                failureNum++;
                String msg = "<br/>" + failureNum + "、派单 " + record.getProductNo() + " 导入失败：";
                failureMsg.append(msg).append(e.getMessage());
                log.error(msg, e);
            }
        }
        if (failureNum > 0) {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        } else {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }

}
